package com.itboye.eweilisten.me.zhanghu;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.popwindow.UIAlertView;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 修改密码
 */
public class UpdatePassActivity extends BaseActivity implements UIAlertView.ClickListenerInterface, Observer {
    ImageView back;
    TextView tvTitle;
    TextView update_pass_zh;
    EditText update_pass_old, update_pass_new, update_pass_new02;
    Button update_pass_ok;
    private UIAlertView uiAlertView;
    UserPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pass);
        presenter = new UserPresenter(this);
        tvTitle.setText(R.string.update_pass);
        init();
    }

    @Override
    public void doLeft() {
        uiAlertView.dismiss();
    }

    @Override
    public void doRight() {

    }

    public void init() {
        String userNmae = (String) SPUtils.get(MyApplication.instance, null,
                Const.EMAIL, "");
        if (!TextUtils.isEmpty(userNmae) && userNmae.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < userNmae.length(); i++) {
                char c = userNmae.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            update_pass_zh.setText(sb.toString());
        }

    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.update_pass_ok:
                String oldPwd = update_pass_old.getText().toString();
                String newpwd = update_pass_new.getText().toString();
                String okNewpwd = update_pass_new02.getText().toString();
//                if (oldPwd == null || oldPwd.equals("")) {
//                    uiAlertView = new UIAlertView(this, "温馨提示！", "您输入的原密码有误请重新输入！", "好", "", type);
//                    uiAlertView.show();
//                    uiAlertView.setClicklistener(this);
//                    return;
//                }
                if (oldPwd.equals("") || oldPwd == null) {
                    MAlert.alert("请输入原密码");
                    return;
                }
                if (newpwd.equals("") || newpwd == null) {
                    MAlert.alert("请输入新密码");
                    return;
                }
                if (!newpwd.equals(okNewpwd)) {
                    MAlert.alert("两次新密码不一样");
                    update_pass_new02.setText("");
                    return;
                }
                if (newpwd.length()!=6) {
                    MAlert.alert("请输入6位新密码");
                    return;
                } else {
                    showProgressDialog("修改中...", true);
                    presenter.onUpdatePwd("itboye", IsStringSPUtils.IsUid(), oldPwd, newpwd);
                }
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.UpdatePwd_success) {
                MAlert.alert(entity.getData() + "");
                finish();
            }
            if (entity.getEventType() == presenter.UpdatePwd_fail) {
                MAlert.alert(entity.getData() + "");
            }
        }
    }
}
